package uk.ac.lkl.common.util.ds;



import uk.ac.lkl.common.util.Handle;


// not based on ClassSingleton. Needs to be reworked eventually.
public class ListHandle extends Handle {

    private Class<?> listClass;


    public ListHandle(Class<?> listClass) {
        this(null, listClass);
    }


    public ListHandle(String name, Class<?> listClass) {
        super(name);
        this.listClass = listClass;
    }


    public Class<?> getListClass() {
        return listClass;
    }


    public String getListClassName() {
        return getListClass().getName();
    }


    // could cache this
    public int hashCode() {
        int hashCode = listClass.hashCode();
        String name = getName();
        if (name != null)
            hashCode += name.hashCode();
        return hashCode;
    }


    public boolean equals(Object object) {
        if (!(object instanceof ListHandle))
            return false;

        ListHandle other = (ListHandle) object;

        String thisName = this.getName();
        String otherName = other.getName();

        boolean listClassesEqual = this.listClass == other.listClass;

        if (thisName == null) {
            if (otherName == null)
                return listClassesEqual;
            else
                return false;
        }
        else {
            if (other.getName() == null)
                return false;
            else
                return listClassesEqual;
        }
    }
}
